package com.wisdudu.blank.support.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.List;

/**
 * Created by pc on 2016/4/13.
 */
public class MyLineGraphView extends View {
    private Context myContext;
    public int paddingLeft;
    public int paddingRight;
    public int paddingBottom;
    public int paddintTop;

    int width;  //控件宽度
    int height; //控件高度
    List<Float> listDate;  //数据源
    double maxDate;  //集合最大值,占满整个高度
    int jgwidth; //2点间隔宽度
    boolean[] isVISIBLE;
    public int index;  //当前选中的index;

    int[] x;
    int[] y;

    boolean isFist=true;       //第一次进来,用于判断点显示不显示要用的
    boolean isInit=false;      //是否已经传过来初始化所有参数,没有的话,view不显示,ondraw里面判断
    MyLineGraphView.Type type;
    int buleIndex;  //需要标蓝的




    public MyLineGraphView(Context context) {
        super(context);
        this.myContext=context;
    }

    public MyLineGraphView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.myContext=context;
    }

    public MyLineGraphView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.myContext=context;
    }


    public void setInit(List<Float> listDate, MyLineGraphView.Type type){
        this.listDate=listDate;
        this.type=type;
        isInit=true;

        this.buleIndex=listDate.size()-1;
        initDatas();
        invalidate();

    }


    public static enum Type{
        week,month,year
    }
    /**
     * 初始化数据
     */
    public void initDatas(){
        maxDate=getMaxint();  //算出最大的数


        paddingLeft=30;
        paddingRight=30;
        paddingBottom=10;
        paddintTop=120;
        x=new int[listDate.size()];
        y=new int[listDate.size()];
        isVISIBLE=new boolean[listDate.size()];

        jgwidth=(getWidth()-paddingLeft-paddingRight)/(listDate.size()-1);

        //算出来个坐标点
        for(int i=0;i<listDate.size();i++){
            double kuan=getMyWeight(i);
            double gao=getMyHeight(listDate.get(i));
            x[i]=(int)kuan;
            y[i]=(int)gao;
//            if(maxDate==0){
//                //如果最大的0;
//                y[i]=getHeight()/2;
//            }
        }

        for (int i=0;i<isVISIBLE.length;i++){
            if(i==isVISIBLE.length-1){
                isVISIBLE[i]=true;
            }else{
                isVISIBLE[i]=false;
            }
        }
        index=listDate.size()-1;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.e("-->","进入冲绘");
        Log.e("-->buluiNDE",buleIndex+"");
        if(!isInit){
            return;
        }
        if (isFist){
            initDatas();
            isFist=false;
        }

        //绘制任意多边形
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#10a679"));
        //设置圆弧的宽度
        paint.setStrokeWidth(10);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);
        Path path = new Path();
        path.moveTo(((int)x[0]), (int)y[0]);

        for(int i=1;i<listDate.size();i++){
            path.lineTo(x[i], y[i]);
        }
        path.lineTo(getWidth()-paddingLeft,getHeight());
        path.lineTo(paddingLeft,getHeight());
        path.lineTo(((int)x[0]), (int)y[0]);
        paint.setStyle(Paint.Style.FILL);

        Shader mShader=new LinearGradient(0,0,0,getHeight(),
                new int[]{0x00BFFF,Color.parseColor("#f2f5f4")},
                null,Shader.TileMode.REPEAT);
        paint.setShader(mShader);
        canvas.drawPath(path, paint);


        //绘制蓝色连线
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#49acd5"));
        //设置圆弧的宽度
        paint.setStrokeWidth(3);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for(int i=0;i<listDate.size();i++){
            double kuan=getMyWeight(i);
            double gao=getMyHeight(listDate.get(i));
            if(i!=0){
                double kuan1=getMyWeight(i-1);
                double gao1=getMyHeight(listDate.get(i-1));
                canvas.drawLine((float) kuan1,(float) gao1,(float) kuan,(float) gao,paint);
            }
        }


        /**
         * 画圆外部蓝色
         */
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#49acd5"));
        //设置圆弧的宽度
        paint.setStrokeWidth(1);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for(int item=0;item<listDate.size();item++){
            //定义一个矩形
            RectF rf2 = new RectF(x[item]-11, y[item]-11, x[item]+11, y[item]+11);
            //画圆
            canvas.drawOval(rf2, paint);
        }


        /**
         * 画圆内部白色
         */
        paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.WHITE);
        //设置圆弧的宽度
        paint.setStrokeWidth(1);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.FILL);
        //消除锯齿
        paint.setAntiAlias(true);
        for(int item=0;item<listDate.size();item++){
            //定义一个矩形
            RectF rf2 = new RectF(x[item]-7, y[item]-7, x[item]+7, y[item]+7);
            //画圆
            canvas.drawOval(rf2, paint);
        }



        //绘制文字
        paint = new Paint();
        paint.setFakeBoldText(true); //true为粗体，false为非粗体
        //mp.setTextSkewX(-0.5f); //float类型参数，负数表示右斜，整数左斜
        //mp.setUnderlineText(true); //true为下划线，false为非下划线
        //mp.setStrikeThruText(true); //true为删除线，false为非删除线
        paint.setTextSize(24); //设置字体大小，int型，如12
        paint.setShader(null);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        for (int i=0;i<x.length;i++){
            if(isVISIBLE[i]){
                if(i==buleIndex){
                    paint.setColor(Color.parseColor("#49acd5"));
                }else{
                    //paint.setColor(Color.parseColor("#b5c3ce"));
                    paint.setColor(Color.BLACK);
                }
                if(type== Type.week){
                    canvas.drawText(listDate.get(i)+"度",x[i]-30,y[i]-30,paint);
                }
            }
            // canvas.drawText(listDate.get(i).getDate(),x[i]-30+paddingLeft/2,getHeight(),paint);
        }


        //绘制文字背景
        paint=new Paint();
        paint.setAntiAlias(true);                       //设置画笔为无锯齿
        paint.setColor(Color.parseColor("#b5c3ce"));
        paint.setStrokeWidth((float) 1.5);              //线宽
        paint.setStyle(Paint.Style.STROKE);                   //空心效果
        paint.setAntiAlias(true);
        for (int i=0;i<x.length;i++){
            if(i==buleIndex){
                paint.setColor(Color.parseColor("#49acd5"));
            }else{
                paint.setColor(Color.parseColor("#b5c3ce"));
            }
            if(isVISIBLE[i]){
                if(type== Type.week) {
                    RectF r1 = new RectF(x[i] - 40, y[i] - 60, x[i] + 40, y[i] - 20);
                    canvas.drawRoundRect(r1, 10, 10, paint);
                }
            }
        }
    }


    public double getMaxint(){
        double max=0;
        for (int i=0;i<listDate.size();i++){
            if(listDate.get(i)>max){
                max=listDate.get(i);
            }
        }
        return max;
    }

    //穿入数据源返回数据源应有的高度
    public double getMyHeight(double d){
        int height=getHeight()-paddintTop-paddingBottom;
        if(maxDate==0){
            return (height-(height*(d/1)))+paddintTop;
        }else{
            return (height-(height*(d/maxDate)))+paddintTop;
        }

    }



    public double getMyWeight(int posotion){
        return posotion*jgwidth+paddingLeft;
    }



    boolean pd1=false;
    boolean pd2=false;
    float mx1,mx2,my1,my2;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:  //按下 = 0
                mx1=event.getX();
                my1=event.getY();
                if(pd1){
                    pd1=false;
                }
                pd1=true;
                Log.e("-->","按下");
                break;
            case MotionEvent.ACTION_MOVE:  //移动 = 2
                pd2=true;
                Log.e("-->","移动");
                break;
            case MotionEvent.ACTION_UP:    // 抬起 = 1
                mx2=event.getX();
                my2=event.getY();
                float number=mx1-mx2>0?mx1-mx2:mx2-mx1;
                if(pd1&pd2&number>10){
                    if(number>10){
                        Log.e("-->","滑动");
                        if((mx1-mx2)>0){
                            Log.e("-->","往左边滑动");
                            slidingEvent.sliding(index,index>0?index-1:index);
                            index=index>0?index-1:index;
                        }else{
                            Log.e("-->","往右边滑动");
                            slidingEvent.sliding(index,index<listDate.size()-1?index+1:index);
                            index=index<listDate.size()-1?index+1:index;
                        }
                        invalidate();
                    }
                }else{
                    int index=isok(event.getX(),event.getY());
                    if(index!=-1){
                        //有点击事件
                        //Toast.makeText(myContext,"点击的第"+(index+1), Toast.LENGTH_SHORT).show();
                        if( isVISIBLE[index]){
                            isVISIBLE[index]=false;
                        }else{
                            isVISIBLE[index]=true;
                        }
                    }
                    invalidate();
                    Log.e("-->","抬起");
                }
                break;
        }

        //gestureDetector.onTouchEvent(event);
        return true;
    }
    //判断点和矩形是否相交
    public boolean isXiangjiao(float dx,float dy,int juBeginX,int juBeginY,int juEndX,int juEndY) {
        if(dx>=juBeginX&dy>=juBeginY&dx<=juEndX&dy<=juEndY){
            return true;
        }else{
            return false;
        }
    }

    //判断点击的坐标是否在个圆圈里面,是则返回圈的下标,不是返回-1
    public int isok(float djx,float djy){
        for(int i=0;i<x.length;i++){
            if(isXiangjiao(djx,djy,x[i]-30, y[i]-60, x[i]+30, y[i]+30)){
                return i;
            }
        }
        return -1;
    }



    public SlidingEvent slidingEvent;


    public static interface SlidingEvent{
        public abstract void sliding(int beforeIndex, int afterIndex);

    }

    public void setSlidingEvent(SlidingEvent slidingEvent){
        this.slidingEvent=slidingEvent;
    }


    public void setBuleIndex(int index){
        this.buleIndex=index;
        Log.e("-->isVisible:",isVISIBLE.length+"");
        for(int i=0;i<isVISIBLE.length;i++){
            isVISIBLE[i]=false;
        }
        isVISIBLE[index]=true;
        invalidate();
    }
}